package com.cskaoyan.service;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.AdminExample;
import com.cskaoyan.bo.AdminCreateBo;
import com.cskaoyan.bo.AdminDeleteBo;
import com.cskaoyan.bo.AdminUpdateBo;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.utils.DateUtils;
import com.cskaoyan.vo.AdminVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdminServicelmpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    /**
     * 功能:管理员页面
     * 作者:whj
     */
    @Override
    public AdminVo query(Integer page, Integer limit, String username, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //排序
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(sort + " " + order);
        //根据username查询
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if(username != null && !"".equals(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        //剔除逻辑删除的管理员
        criteria.andDeletedEqualTo(false);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        //符合条件的total
        PageInfo<Admin> adminPageInfo = new PageInfo<>(admins);
        long total = adminPageInfo.getTotal();
        //返回结果
        AdminVo adminVo = new AdminVo();
        adminVo.setItems(admins);
        adminVo.setTotal(total);
        return adminVo;
    }

    /**
     * 功能:管理员页面,/admin/create
     * 作者:whj
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public Admin create(AdminCreateBo adminCreateBo) {
        Admin admin = new Admin();
        admin.setUsername(adminCreateBo.getUsername());
        admin.setPassword(adminCreateBo.getPassword());
        admin.setRoleIds(adminCreateBo.getRoleIds());
        admin.setAddTime(new Date(System.currentTimeMillis()));
        admin.setUpdateTime(new Date(System.currentTimeMillis()));
        admin.setAvatar(adminCreateBo.getAvatar());
        admin.setDeleted(false);
        //先判断数据库里是否有同名的用户,有则添加失败
        Admin result = adminMapper.selectAdminByUsername(admin);
        if(result != null){
            return null;
        }
        adminMapper.insertBackId(admin);
        admin.setDeleted(null);
        return admin;
    }

    /**
     * 功能:管理员页面,/admin/update
     * 作者:whj
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public Admin update(AdminUpdateBo adminUpdateBo) {
        Admin admin = new Admin();
        admin.setId(adminUpdateBo.getId());
        admin.setUsername(adminUpdateBo.getUsername());
        admin.setPassword(adminUpdateBo.getPassword());
        admin.setRoleIds(adminUpdateBo.getRoleIds());
        //String转为Date
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date addTime = new Date();
        try {
            addTime = dateFormat.parse(adminUpdateBo.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        admin.setAddTime(addTime);
        admin.setUpdateTime(new Date(System.currentTimeMillis()));
        admin.setAvatar(adminUpdateBo.getAvatar());
        admin.setDeleted(false);
        adminMapper.updateByPrimaryKey(admin);
        admin.setDeleted(null);
        return admin;
    }

    /**
     * 功能:管理员页面,/admin/delete
     * 作者:whj
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public void delete(AdminDeleteBo adminDeleteBo) {
        Admin admin = new Admin();
        admin.setId(adminDeleteBo.getId());
        admin.setUsername(adminDeleteBo.getUsername());
        admin.setPassword(adminDeleteBo.getPassword());
        admin.setRoleIds(adminDeleteBo.getRoleIds());
        //String转为Date
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date addTime = null;
        try {
            addTime = dateFormat.parse(adminDeleteBo.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        admin.setAddTime(addTime);
        admin.setUpdateTime(new Date(System.currentTimeMillis()));
        admin.setAvatar(adminDeleteBo.getAvatar());
        admin.setDeleted(true);
        adminMapper.updateByPrimaryKey(admin);
        admin.setDeleted(null);
    }
}
